Repository navigation
Fix truncated requests and responses in the Codex API relay - #8713
loopx-agent wants to merge 7 commits into
Conversation
Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
Port the verified response framing fix to the request-admission branch. Original candidate 27bc3aee311cdfc6a00466ff9d3b58c7db36e01b. Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
loopx-agent
left a comment
There was a problem hiding this comment.
Reviewer: model_agent · gpt-6.1-sol · OpenAI · runtime_reported · reasoning_effort=max
Request changes conclusion (author-owned PR; GitHub blocks formal self-review)
评审提交 e50c0d8c1256881aeea36f3204a5dd27c52e0ec8,目标主干 78e936f9d1f1a00505c4026ee0a5857de95cee27。
[P2] 上游缺少 chunked 结束空行时仍报完成并允许换 key。 实际 loopback urllib 上游与 HTTP/1.1 下游中,200 前缀被重新包成完整传输。429 路径还用第二把合成 key 向上游发送了相同请求,下游返回第二次 200。完全缺少零长度 chunk 时则保持短流,429 返回 502 且仅调用一次。相同的半截结尾在目标主干也能触发两路错误。 现有上游读取边界须获得实际完整的 framing 证据。未收到 trailer section 结束空行时,保留已收到的成功流前缀并让下游继续观察到截断。对错误体按未知上游结果返回 502,避免进入换 key 政策。 发生位置
动机 / Motivation
既有 relay 会在部分传输后给出看似完整的读回,或依据解析出来的 quota JSON 尝试下一把 key。候选的请求帧和停机保护已有明确改进。 使用可选 Codex API relay 的 worker、steward 和 compact 调用者。 已启用本地 relay。上游把完整可解析的输出前缀或 429 JSON 放进 chunked 响应,发送零长度 chunk 行后连接关闭,缺少结束 trailer section 的 CRLF。 实际 loopback urllib 上游与 HTTP/1.1 下游中,200 前缀被重新包成完整传输。429 路径还用第二把合成 key 向上游发送了相同请求,下游返回第二次 200。完全缺少零长度 chunk 时则保持短流,429 返回 502 且仅调用一次。相同的半截结尾在目标主干也能触发两路错误。 下游可能把传输失败当作正常结束。未经验证的错误体还触发一次额外模型请求,超出此处原有重试条件。 独立验证使用当前源码和合成环回上游、合成 key。已安装 Codex、真实上游和完整成本未验证。
改动思路 / Approach
按实际调用旅程从认证入站、预读、接收结束到回环读回核验。原 relay 在准入前检查固定请求长度,对在途与接收中的 socket 分别 drain,并保留响应长度或重包 chunk。错误体仍由原 key 政策分类。 修复限于现有本地 relay 的 HTTP 传输与安全重试边界。保持现有模型与 key 策略 owner。 现有上游读取边界须获得实际完整的 framing 证据。未收到 trailer section 结束空行时,保留已收到的成功流前缀并让下游继续观察到截断。对错误体按未知上游结果返回 502,避免进入换 key 政策。 HTTP 结束条件见 RFC 9112 §7.1。
具体改动 / Changes
完整差异 5 个文件,新增 398 行、删除 38 行。一个生产 relay 模块、两组回环和生命周期测试,以及操作说明与既有修复案例。
关键代码讲解
- _forward 的 HTTPError 读取。读取有上限的错误体并把已验证的错误交给 key 策略。 length=None 时,urllib 的 EOF 会掩盖缺失的最后空行。 429 后多发一条相同请求。 源码
- _forward 的流结尾。流式传递已经接收的成功响应并完成下游 framing。 上游宽松解码返回 EOF 后无条件写出结束 chunk。 客户端报告传输完整。 源码
- Relay 的传输边界。记录可选 relay 对短请求和短响应的职责。 现有承诺要求截断可见,错误体确认后才能换 key。 补足现实协议边界后仍用同一选项和策略。 源码
验收沿用 原有契约。结果由本地真实入口读回。
original-boundary。沿原读写 owner,真实可验证的来源与响应。纠正归档快照的文件模式后,42 项精确源码测试通过。独立回环完整结尾与完全缺失尾块的四个对照均符合预期。boundary-negative。上游缺少 chunked 结束空行时仍报完成并允许换 key。实际 loopback urllib 上游与 HTTP/1.1 下游中,200 前缀被重新包成完整传输。429 路径还用第二把合成 key 向上游发送了相同请求,下游返回第二次 200。完全缺少零长度 chunk 时则保持短流,429 返回 502 且仅调用一次。相同的半截结尾在目标主干也能触发两路错误。 用真实两段 loopback HTTP 对比无尾块、仅零长度 chunk 行、完整结尾三种响应。200 检查原前缀与传输完整态,429 检查 502 和一次请求。完整 429 继续走现有 key 政策。
对主干的风险 / Risks
纠正归档快照的文件模式后,42 项精确源码测试通过。独立回环完整结尾与完全缺失尾块的四个对照均符合预期。 功能由现有本地启用与 fallback 策略控制。定向测试覆盖关闭、认证拒绝及停止新准入;已有成功与诊断路径继续通过。
用真实两段 loopback HTTP 对比无尾块、仅零长度 chunk 行、完整结尾三种响应。200 检查原前缀与传输完整态,429 检查 502 和一次请求。完整 429 继续走现有 key 政策。 本次验证使用本地定向检查。
我的整体评价 / Verdict
下游可能把传输失败当作正常结束。未经验证的错误体还触发一次额外模型请求,超出此处原有重试条件。 最小修复见上。独立验证使用当前源码和合成环回上游、合成 key。已安装 Codex、真实上游和完整成本未验证。
English verdict: REQUEST_CHANGES at e50c0d8c1256881aeea36f3204a5dd27c52e0ec8. An upstream chunked response ending after the zero-size line without a final CRLF is accepted as complete. The 200 prefix gets clean downstream framing and a 429 causes another upstream POST. Require valid closure before emitting the final chunk or consulting key-rotation policy.
| remaining = getattr(error.fp, "length", None) | ||
| # A bounded read can silently reach early EOF, and a | ||
| # body larger than our limit cannot be verified in full. | ||
| if encoded is None or len(encoded) > ERROR_BODY_LIMIT or (remaining or 0) > 0: |
There was a problem hiding this comment.
[P2] 上游缺少 chunked 结束空行时仍报完成并允许换 key。 实际 loopback urllib 上游与 HTTP/1.1 下游中,200 前缀被重新包成完整传输。429 路径还用第二把合成 key 向上游发送了相同请求,下游返回第二次 200。完全缺少零长度 chunk 时则保持短流,429 返回 502 且仅调用一次。相同的半截结尾在目标主干也能触发两路错误。 现有上游读取边界须获得实际完整的 framing 证据。未收到 trailer section 结束空行时,保留已收到的成功流前缀并让下游继续观察到截断。对错误体按未知上游结果返回 502,避免进入换 key 政策。
Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
loopx-agent
left a comment
There was a problem hiding this comment.
Reviewer: model_agent · gpt-6.1-sol · OpenAI · runtime_reported · reasoning_effort=max
Request changes conclusion (author-owned PR; GitHub blocks formal self-review)
评审提交 700bb92f3b8d63064bb03b27677f1697e74a5441,对照目标主干 8caf2da6a52d37f722703a02d79f9a70711b5bd1。
[P2] 校验上游每个数据块的 CRLF 后再结束响应或换 key。 当前覆写只核对 trailer 末尾,继承的 HTTPResponse 会丢弃数据块后的两个字节。实际 TLS 上游把这里的 CRLF 换成 XX,保留合法零 chunk 和终止空行。200 的 SSE 前缀被封装成完整传输。429 触发两次相同的上游 POST、两把合成 key,并设置第一把 key 的 quota cooldown。请在现有解析入口确认数据块分隔符。200 让客户端收到原前缀并观察到传输失败。429 返回 502,保持一次上游尝试。 HTTP/1.1 framing
动机 / Motivation
启用了 Codex API relay 的 worker、steward 和 compact,以及排查中断的操作者。 目标主干在上游 chunked 返回缺少 trailer 空行或数据块分隔符无效时,把 200 前缀交给下游作为完整传输,也接受同样封装的 429 并尝试第二把 key。 同一请求在未知上游结果后占用第二把 key,并改变第一把 key 后续可用性。先前请求的实际效果仍待确认。 合成 TLS 源把一个完整数据块后的 CRLF 换成 XX,保留后续零长度 chunk 与 trailer 的最终空行。数据体分别为 SSE 前缀和完整的 429 quota JSON。 当前 head 的缺 trailer 空行用例已恢复正确行为。200 保留 SSE 前缀并报 IncompleteRead,429 返回 502、上游只收一次,无 quota cooldown。数据块分隔符错误时,当前 head 和主干仍把 200 前缀视为完整。429 发出两次 POST、使用两把不同合成 key、写入第一把 key 的 quota cooldown,最终返回第二次 200。 完整请求长度检查与 drain 生效。当前 HTTPS handler 验证最后的 trailer 空行,已修复上轮发现。数据块分隔符仍由标准库消费,回环 TLS 复验可重复。 采用当前可选 relay、既有模型选路和持久状态。 仅在两个隔离源码工作树上通过真实 loopback HTTP、TLS 和合成 key 复验。已安装 Codex、真实上游及受托费用待验证。
改动思路 / Approach
可选 relay 的完整 HTTP 帧检查、drain 和对原 policy 的回执。 已认证请求先确认唯一 Content-Length 和完整请求体,再占用模型准入并开始上游调用。Drain 释放未提交的读取,给已准入的响应独立的本地写超时。响应保持长度或使用新的下游 chunk framing。在 relay 已有 HTTPS 响应解析入口核验数据块后的 CRLF,并沿用终止空行验证。边界损坏时让现有读取异常结束下游传输或返回 502,在错误分支调用 key 策略前结束。 RFC 9112 §7.1
具体改动 / Changes
本次差异 5 个文件,新增 557 行、删除 40 行。生产 relay 变更有 128 行新增、26 行删除。两套测试覆盖读写停顿、入站 framing、上游截断和冷却策略。操作文档把 transport 读回与模型完成分开,修复案例只在排查此故障时提供诊断步骤。
在目标主干 8caf2da6a52d37f722703a02d79f9a70711b5bd1 的 packages/loopx-codex-provider-routing/NATIVE.md 核对 Authenticated relay、Drain admitted requests 和 Unknown upstream effects。前两项由现有凭据门、两次 drain 核对和在途计数保留。第三项在无效 chunk 分隔符路径仍有缺口。 目标主干操作契约
关键代码讲解
- RelayHandler.do_POST 在读取完整请求体和核对 drain 后才递增 active_requests。持有未完整请求的 socket 仍可由关闭路径停止读取。 请求边界
- _RelayHTTPResponse 的 trailer 终止检查已有两个失败对照。继承的 chunk 读取仍把分隔符当成任意两个字节消耗。 响应解析
- RelayHandler._forward 将 HTTPError 解码结果交给原 key 政策。真实故障走过了 cooldown 和第二次调用。200 流同一解析问题会写出最后的下游 chunk。 拒绝分类 流结尾
对主干的风险 / Risks
- 对照用同一套真实环回 TLS,在主干和候选各重放有效 framing、缺 trailer 空行及错分隔符,分别测 200 和 429。完整 200 只有一次请求。完整 429 保留一次合法 fallback。缺 trailer 空行在候选已确认修复。
- 51 个 relay 和 lifecycle 测试通过,包括超时、半开读写 drain、限长和禁用态。Quick premerge 的 5 个直接检查和 3 个选定检查通过,提交含 DCO。
- 功能沿原可选 API 配置,关闭时订阅维持经原 owner 选路。诊断指引只随该故障案例加载。现有 key 政策、状态格式和循环调度接口继续复用。边界错误会积累额外的请求与冷却;已安装宿主和真实 OpenAI 未验证。
我的整体评价 / Verdict
请求更改。完整请求及 drain 有实质改善,候选已解决上次的 trailer 终止问题。仍需把无效数据块分隔符保留为未完整的传输,才能让下游正确恢复并遵守未知效果的重试边界。在现有真实 socket framing 测试按状态参数化有效 CRLF、缺 trailer 空行和 XX 数据块分隔符。核对 200 原始前缀、IncompleteRead 和一次上游尝试,429 核对 502、一次上游尝试及无 cooldown。保留正常 429 的两次上游尝试作为对照。
English verdict: REQUEST_CHANGES. Reject malformed chunk boundaries before completing a downstream transfer or authorizing another key.
|
|
||
|
|
||
| class _RelayHTTPResponse(HTTPResponse): | ||
| def _read_and_discard_trailer(self): |
There was a problem hiding this comment.
[P2] 校验上游每个数据块的 CRLF 后再结束响应或换 key。 当前覆写只核对 trailer 末尾,继承的 HTTPResponse 会丢弃数据块后的两个字节。实际 TLS 上游把这里的 CRLF 换成 XX,保留合法零 chunk 和终止空行。200 的 SSE 前缀被封装成完整传输。429 触发两次相同的上游 POST、两把合成 key,并设置第一把 key 的 quota cooldown。请在现有解析入口确认数据块分隔符。200 让客户端收到原前缀并观察到传输失败。429 返回 502,保持一次上游尝试。 HTTP/1.1 framing
|
This pull request has merge conflicts with Choose the remote for the base repository, not an out-of-date fork. git fetch upstream
git rebase upstream/main
# Resolve each conflict, git add the resolved files, then git rebase --continue.
git push --force-with-lease origin HEADFor a same-repository clone whose Keep the DCO |
Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
loopx-agent
left a comment
There was a problem hiding this comment.
Reviewer: model_agent · gpt-6.2-sol · OpenAI (runtime_reported, max)
Approval conclusion (author-owned PR; GitHub blocks formal self-approval)
动机 / Motivation
使用可选 OpenAI relay 的 Codex 客户端需要区分已收到模型输出与确已传输完成,也需要在中继退出后安全接续。既有中继会把部分输出收成正常 EOF,或在错误正文不完整时切换 key。本次提交在请求准入、上游 framing 与下游输出上保留传输边界。客户端保留已收前缀,截断仍可观察。未完整收到的拒绝回 502,完整拒绝沿用既有 key 策略。独立验证使用合成 key 和 loopback。真实已安装环境与上游采用另待验证。
改动思路 / Approach
本地 RelayHandler 在完整接收入站正文后才登记模型请求。HTTPS 读取沿用 urllib 和 HTTPResponse 的既有解析,在回复数据块与 trailer 收尾增加检查。下游按长度或 chunked 发出已收到的内容。五个文件覆盖可选 relay、生命周期回归、传输测试和相关文档。请求正文和 drain 共用同一准入锁。上游 parser 复用既有 chunk 读取,确认分隔符与结束空行后才让下游收尾。错误正文完整性在 key policy 前判断。
具体改动 / Changes
精确 head 19036f034d866c64de34ba7703faf61a1f77e162,diff base 45c015ffd9c9065b3af533e0abf103170627d15f,5 个文件 +571/-39。逐项复核 complete-request-admission、incomplete-transfer-visible、verified-rejection-policy。验收约定
关键代码讲解
- 自定义 HTTPResponse 补齐数据块分隔符和 trailer 结束的检查,继续复用标准读取方法。源码
- drain 只关闭未准入连接的读侧。已准入转发使用单独的本地写期限与上游期限。源码
- 转发保留已读字节。错误正文完整后才进入现有 key policy。源码
对主干的风险 / Risks
用同一组合 fixture 比较主干和当前提交。基线把九个截断或未完整拒绝用例当成成功或进入换 key。候选 16 项全过,包括原发现的 chunk delimiter、终止空行、已收 SSE 前缀以及完整 429 的对照。拒绝请求正文异常时上游零调用,接收端停止读取时本地 drain 可结束,已准入的健康请求仍可回读。 RFC 9112 §7.1
语义与 CI 对齐
候选的源码套件 53 项通过,基线既有套件 28 项通过。相同 fixture 的两边语义与现有启用条件一致。改动文件 Ruff 和 diff 检查通过。两边的 Monitor poll smoke 都在同一断言返回意外成功,全库 Ruff 和配置中的 mypy 亦逐字相同。当前评审仅涉及本地可归因的传输行为,真实安装接续待核验。
我的整体评价 / Verdict
当前提交修复了已知的传输截断与未完成拒绝换 key 问题,保留正常流的前缀、终止和已授权的重试,批准此精确提交。既有 Monitor poll 和静态门继续单列,严格质量与真实采用待回读。
English verdict: APPROVE. Approve 19036f0. Verified framing preserves received bytes and incomplete status, while incomplete upstream rejections cannot trigger another key. Complete rejection and lifecycle controls pass. Baseline quality holds and live adoption remain separate.
Current revision
The relay validates the delimiter after each upstream data chunk before treating the response as complete. An interrupted stream preserves the bytes already received and keeps the downstream transport incomplete. An incomplete upstream rejection follows the existing 502 path with one upstream attempt. A fully framed rejection keeps the established key rotation policy. HTTP/1.1 chunk framing Exact-head finding
中继现在逐个验证上游数据块的结尾。已收到的流式数据保留,未完整收到的拒绝响应按原有 502 路径处理。完整上游拒绝仍可按现行策略切换 key。
Validation at
19036f034d86The tested main comparison contains five files. Exact comparison
Earlier revisions and evidence
What changed
The optional Codex OpenAI relay now verifies that the inbound request body matches its framing before it can send a model request. Previously, a client that closed its write side early could send just enough JSON to trigger an upstream request, despite declaring a longer body. A socket waiting for the rest of a request could also prevent the relay from releasing its owner lock during a restart.
Worker, steward, and compact routes reject incomplete or ambiguously framed bodies before dispatch. Drain closes sockets that are still reading requests and waits for model requests that were admitted before drain. Idle request reads time out after 10 seconds.
On the response side the relay keeps explicit HTTP transfer framing so clients can detect a short response after receiving model output. It only passes a verified complete error body to the existing key policy. A truncated quota error therefore cannot trigger an attempt with another key.
A caller that stops reading an admitted response could also keep the old relay's instance lock held. The response now keeps the existing ten-second timeout on loopback writes. Upstream reads keep their own timeout. A blocked write ends the incomplete HTTP transfer, and the relay can release its lock after the other admitted requests finish.
The relay now requires the blank line that ends the upstream chunked trailer section. If the origin closes early, the client gets the already received success prefix as an incomplete transfer. An incomplete quota rejection returns 502 and keeps the original dispatch count at one. A fully framed quota rejection still uses the existing key policy.
Validation
Current head
700bb92f3b8d63064bb03b27677f1697e74a5441, composed with main8caf2da6a52d37f722703a02d79f9a70711b5bd1.Previous head validation
The tests use local synthetic responses and credentials. The native Codex fixture runs with client retries disabled to count each operation.
Author Declaration
Written by OpenAI Codex. The reproduced framing, shutdown, and response-delivery cases and the existing native relay operator contract define this repair. Native relay contract
Frontend / Visual Evidence
UI impact none. This change is in the existing worker, steward, and compaction HTTP transport.
Scope And Continuation
The 10-second limit applies to each local socket operation. The timing comparison preserves independent upstream waits and distinguishes a client transfer ending from upstream execution state. Validation uses disposable environments and synthetic credentials.